package com.dongchedi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongchedi.entity.CustomerAddr;
import com.dongchedi.service.CustomerAddressService;
import com.dongchedi.utils.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * @author leibidong
 * @description   用户地址controller层
 * @date 2021/6/24
 */
@Controller
@RequestMapping("api")
@ResponseBody
public class CustomerAddressController {
    @Autowired
    CustomerAddressService customerAddressService;
    /*
     * @Author: leibidong
     * @Description: 查询用户的所有地址信息
     * @Date: 2021/6/26 20:04
     * @Param: [customerId, pageNum, pageSize]
     * @Return: java.lang.String
     **/
    @GetMapping("/select/address/detail")
    public String selectAddressDetail(Integer customerId,Integer pageNum,Integer pageSize){
        JSONArray jsonArray=new JSONArray();
       jsonArray=customerAddressService.queryAllAddress(customerId,pageNum,pageSize);
        if(!jsonArray.isEmpty()) {
            return ResultMessage.getJSONArrayString(1, "地址用户查询成功",jsonArray );
        }else {
            return ResultMessage.getJSONArrayString(0, "地址用户查询失败",jsonArray );
        }
    }
    /*
     * @Author: leibidong
     * @Description:用户添加地址
     * @Date: 2021/6/26 20:11
     * @Param: [customerAddr]
     * @Return: java.lang.String
     **/
    @PostMapping("/add/address/detail")
    public  String addAddress(CustomerAddr customerAddr){
        JSONObject jsonObject=new JSONObject();
        Integer i=customerAddressService.addCustomerAddress(customerAddr);
        if(i==0){
            return ResultMessage.getJSONObjectString(0,"添加失败，请重新添加地址",jsonObject);
        }else if(i==1){
            return ResultMessage.getJSONObjectString(0,"添加失败，地址信息重复",jsonObject);
        }else  {
            jsonObject.put("status","ok");
            return ResultMessage.getJSONObjectString(1,"添加成功",jsonObject);
        }
    }
    /*
     * @Author: leibidong
     * @Description: 用户更新地址
     * @Date: 2021/6/26 20:12
     * @Param: [customerAddr]
     * @Return: java.lang.String
     **/
    @PostMapping("/update/address/detail")
    public String updateAddAddress(CustomerAddr customerAddr){
        JSONObject jsonObject=customerAddressService.updateCustomerAddress(customerAddr);
        if(!jsonObject.isEmpty()){
            return ResultMessage.getJSONObjectString(1,"地址更新成功",jsonObject);
        }else {
            return ResultMessage.getJSONObjectString(0,"地址更新失败",jsonObject);
        }
    }
    /*
     * @Author: leibidong
     * @Description: 用户删除地址
     * @Date: 2021/6/26 20:12
     * @Param: [id, customerId]
     * @Return: java.lang.String
     **/
    @GetMapping("/delete/address/detail")
    public String deleteAddress(Integer id,Integer customerId){
   JSONObject jsonObject=customerAddressService.deleteCustomerAddress(id,customerId);
        if(!jsonObject.isEmpty()){

            return ResultMessage.getJSONObjectString(1,"地址删除成功",jsonObject);
        }else {
            return ResultMessage.getJSONObjectString(0,"地址删除失败",jsonObject);
        }
    }
    /*
     * @Author: leibidong
     * @Description: 用户设置默认地址
     * @Date: 2021/6/26 20:12
     * @Param: [customerId, id]
     * @Return: java.lang.String
     **/
    @PostMapping("/update/default/address")
    public String defaultAddress(Integer customerId,Integer id){
        JSONObject jsonObject=customerAddressService.updateCustomerDefaultAddress(customerId,id);
        if(!jsonObject.isEmpty()){
            return ResultMessage.getJSONObjectString(1,"默认地址更改成功",jsonObject);
        }else {
            return ResultMessage.getJSONObjectString(0,"默认地址更改失败",jsonObject);
        }
    }
}
